package com.example.ch04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class add_activity extends AppCompatActivity {
    private Button btn_submit;

    private EditText et_name;
    private EditText et_sex;
    private EditText et_phone;
    private EditText et_workplace;
    private EditText et_work;
    Create_Datebase cdatebase;
    private SimpleCursorAdapter listviewAdapter;

    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_activity);


        et_name=(EditText)findViewById(R.id.et_name);
        et_sex=(EditText)findViewById(R.id.et_sex);
        et_phone=(EditText)findViewById(R.id.et_phone);
        et_workplace=(EditText)findViewById(R.id.et_workplace);
        et_work=(EditText)findViewById(R.id.et_work);
        cdatebase=new Create_Datebase(this);
        btn_submit=(Button)findViewById(R.id.btn_submit);

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add_date();
                passdate();
            }
        });
    }

    //回传
    public void passdate(){
        Intent intent=new Intent();
//        List<Person> persons=
        setResult(1,intent);
        finish();
    }
    //数据库添加数据
    public void add_date(){
        String name,sex,phone,workplace,work;
        ContentValues values;

        name=et_name.getText().toString();
        sex=et_sex.getText().toString();
        phone=et_phone.getText().toString().trim();
        workplace=et_workplace.getText().toString();
        work=et_work.getText().toString();

        db=cdatebase.getWritableDatabase();//获取可读写的数据库对象
        values=new ContentValues();
        values.put("name",name);
        values.put("sex",sex);
        values.put("phone",phone);
        values.put("workplace",workplace);
        values.put("work",work);
        db.insert("information",null,values);
        Toast.makeText(add_activity.this,"信息已添加",Toast.LENGTH_SHORT).show();
        Log.i("a","添加成功");
        db.close();

    }


    }

